package fractales.ui;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import fractales.algo.Fractale;


public abstract class FractaleUI extends JPanel
{
	public static void display(final Fractale p)
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e) {}
    	
		final JFrame f = new JFrame();
    	
    	MouseWheelListener mwl = new MouseWheelListener()
    	{
			@Override
			public void mouseWheelMoved(MouseWheelEvent e)
			{
				int notch = ((MouseWheelEvent) e).getWheelRotation();
				
				if (e.isShiftDown())
				{
					double currentZoom = p.getZoom();
					double newZoom = currentZoom - notch/100.;
					if (newZoom > 0)
					{
						p.setZoom(newZoom);
						p.repaint();
					}
					
					return;
				}
				
				int newIteration;
				if (notch < 0)
				{
					int currentIteration = p.getIteration();
					newIteration = currentIteration - notch;
					
					p.setIteration(newIteration);
				}
				else
				{
					int currentIteration = p.getIteration();
					if (currentIteration == 1)
						return;
					newIteration = currentIteration - notch;
					if (newIteration < 1)
						newIteration = 1;
				}
				
				p.setIteration(newIteration);
				Thread t = new Thread()
				{
					@Override
					public void run()
					{
						p.createImage();
					}
				};
				
				p.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				t.start();
				try
				{
					t.join();
					p.repaint();
					p.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
				}
				catch (InterruptedException e1)
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
    	};
		p.addMouseWheelListener(mwl);
		
		ComponentListener cl = new ComponentListener()
		{
			@Override
			public void componentHidden(ComponentEvent e) {}

			@Override
			public void componentMoved(ComponentEvent e) {}

			@Override
			public void componentResized(ComponentEvent e)
			{
				Thread t = new Thread()
				{
					@Override
					public void run()
					{
						p.createImage();
					}
				};
				
				p.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				t.start();
				try
				{
					t.join();
				}
				catch (InterruptedException e1)
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				p.repaint();
				p.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}

			@Override
			public void componentShown(ComponentEvent e) {}
		};
		p.addComponentListener(cl);
    	
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	f.setLayout(new BorderLayout());
    	f.setMinimumSize(new Dimension(640,480));
    	f.add(p);
    	
    	f.setVisible(true);
	}
}
